#include "stdio.h"
#define MAXSIZE 20

typedef int datatype;
typedef struct
{
	datatype  data[MAXSIZE];
    int length;
}SqList;
SqList SqL;
int score[10]={77,88,99,60,87,100,50,85,90,56};

void GetElement(SqList &L,int i,datatype &e);
int SearchElement(SqList &L,datatype e);
//函数重载
void ListLength(SqList &L,int &length);
int ListLength(SqList &L);
int ListInsert(SqList &L,int loc,datatype x);//第loc元素后插入
void ShowSqlist(SqList* L);
void LisDeletet(SqList* L,int loc);

int main()
{
  int i,k;
  datatype element;
  SqL.length=0;
  //初始化值，将数组中的数值赋值给结构体
  for (i=0;i<10;i++)
  {
	  SqL.data[i]=score[i];
	  SqL.length++;
  }
  printf("Original list:\n");
  //调用函数，传入SqLsit类型地址
  ShowSqlist(&SqL);
  printf("此时链表的长度为：%d\n",ListLength(SqL));

  element=9;
  printf("\n正在往4号位置插入元素9");
  k=ListInsert(SqL,3,element);//插入1号元素
  ShowSqlist(&SqL);
  printf("此时链表的长度为：%d\n",ListLength(SqL));

  printf("\n正在删除9号元素");
  LisDeletet(&SqL,9);   //删除9号元素
  ShowSqlist(&SqL);
  printf("此时链表的长度为：%d\n",ListLength(SqL));

  GetElement(SqL,3,element);
  printf("\nNo.3 element is %2d\n",element);

  int index=SearchElement(SqL,87);
  printf("ID of 87 is No.%2d\n",index);

  int length;
  ListLength(SqL,length);
  printf("\nNow, Linklist has %2d elements!\n\n",length);

  LisDeletet(&SqL,15);
  length=ListLength(SqL);
  printf("Now, Linklist has %2d elements too!\n",length);/**/
  return 0;
}

int ListInsert(SqList &L,int loc,datatype x)
{
   int i;
   if (L.length<1)
   {
      printf("表长错误,插入失败！\n");
      return(-1);
   }
   if (loc<1||loc>L.length)
   {
      printf("插入位置i不适当,插入失败！\n");
      return(-1);
   }
   for (i=L.length;i>loc;i--)
	   L.data[i]=L.data[i-1];
   L.data[i]=x;

   L.length++;
   printf("\nInsert success!\n");
   return(0);
}

void LisDeletet(SqList* L,int loc)
{
   int i;
   if(loc<1||loc>L->length)
   { printf("Position error!\n");
     return;}

   for(i=loc;i<=L->length;i++)
      L->data[i-1]=L->data[i];

   L->length--;
   printf("\nDelete success!\n");
}

void ShowSqlist(SqList* L)
{
	//遍历
	for (int i=0;i<L->length-1;i++)
        printf("%2d-->",L->data[i]);
    printf("%2d\n",L->data[i]);
}

void GetElement(SqList &L,int i,datatype &e)
{
	e=L.data[i-1];
}

int SearchElement(SqList &L,datatype e)
{
    int i;
	for (i=0;i<L.length;i++)
		if(L.data[i]==e) break;

	if(i==L.length)
	{
		printf("can't found the data\n");
		return(-1);
	}
	else return i+1;
}

void ListLength(SqList &L,int &length)
{
	length=L.length;
}

int ListLength(SqList &L)
{
	int length;
	length=L.length;
	return length;
}


